package com.car.action.admin;

import java.util.List;

import com.car.common.model.MessageUnreadNum;
import com.car.common.model.Resume;
import com.car.common.model.UserFriends;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.utils.StringUtil;
import com.utils.SystemClock;

public class ResumeController extends Controller{

	public void index() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        String andSQL = "";
        if(!StringUtil.empty(getPara("user_id"))) {
        	andSQL += " where r.user_id = " + getPara("user_id");
        }
	    Page<Resume> resumeList = Resume.dao.paginate(page, 30,"select r.id,r.user_id,r.nick_name,r.user_header,r.target_user_id,u.nick_name as target_nick_name,u.user_header as target_user_header ,r.skin,r.weight,r.height,r.position,r.edu,r.income,FROM_UNIXTIME(r.createtime,'%Y-%m-%d %H:%i') as  create_time_string "," from resume as r left join users as u on r.target_user_id = u.id " + andSQL +" order by r.id desc ");
		setAttr("page", resumeList);
		setAttr("user_id", getPara("user_id"));
		render("index.html");
	}
	
	
	public void photo() {
		Resume one = Resume.dao.findById(getPara("id"));
		String[] photos =  one.getUserHeader().split(",");
		setAttr("list", photos);
		render("photos.html");
	}
	
	public void friend() {
		Resume resume = Resume.dao.findById(getPara("id"));
		Integer user_id = resume.getTargetUserId();
		Integer target_user_id = resume.getUserId();
		 //判断是否是好友
		UserFriends one = UserFriends.dao.findFirst("select id from user_friends where (user_id = "+ user_id +" and target_user_id = " + target_user_id +") and status = 1 and delete_time is null order by id desc limit 1 ");
		if(one == null) {
			one = new UserFriends();
			one.setUserId(user_id.intValue());
			one.setTargetUserId(target_user_id);
			one.setStatus(1);
			one.setCreateTime(SystemClock.now()/1000L);
			one.setUpdateTime(SystemClock.now()/1000L);
			if (one.save()) {
				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FRIEND, target_user_id);
			}
			renderText("操作成功!");
		} else {
			renderText("已经是好友了");
		}
	}
}
